package com.book.system.dao;

import com.book.system.entity.User;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @author ：aaa
 * @date ：Created in 2024/4/27 11:44
 */
@Slf4j
public class UserDao {

    /**
     * 登录方法
     *
     * @param user 用户实体类
     * @return list
     */
    public List<Map<String, Object>> login(User user) {
        String sql = "select * from user where username = ? and password = ?";
        log.info("sql:{}", sql);
        List<Map<String, Object>> list = BaseDao.executeQuery(sql, user.getUsername(), user.getPassword());
        return list;
    }

    /**
     * 获取所有用户数据
     *
     * @param name 用户名
     * @param flag 用户类型
     * @return java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @author : oath
     * @create time: 2024/5/14 22:37
     */
    public List<Map<String, Object>> getList(String name, Integer flag) {

        // 拼接查询语句 创建部分sql对象
        StringBuilder tempSql = new StringBuilder("select * from user where 1=1 ");
        //判断用户名字是否为空 不为空则拼接查询语句
        if (name != null && !"".equals(name)) {
            tempSql.append(" and username like '%").append(name).append("%'");
        }
        //判断用户类型是否为空 不为空则拼接查询语句
        if (flag != null && flag != 2) {
            tempSql.append(" and flag = ").append(flag);
        }
        //将创建的sql对象转换为字符串
        String sql = tempSql.toString();
        log.info("sql语句：{}", sql);

        //执行查询语句
        return BaseDao.executeQuery(sql);

    }

    /**
     * 添加用户
     *
     * @param user 用户实体类
     * @return int
     * @author : oath
     * @create time: 2024/5/14 22:37
     */
    public int save(User user) {

        //sql语句
        String sql = "insert into user(name,username,password,flag,address,phone,sex,birth) values(?,?,?,?,?,?,?,?)";
        //执行sql语句
        return BaseDao.executeUpdate(sql, user.getName(), user.getUsername(), user.getPassword(), user.getFlag(), user.getAddress(), user.getPhone(), user.getSex(), user.getBirth());
    }

    /**
     * 根据id修改用户信息
     *
     * @param user 用户实体类
     * @return int
     * @author : oath
     * @create time: 2024/5/14 22:41
     */
    public int updateById(User user) {

        String sql = "update user set name = ?,username = ?,password = ?,flag = ?,address = ?,phone = ?,sex = ?,birth = ? where id = ?";
        return BaseDao.executeUpdate(sql, user.getName(), user.getUsername(), user.getPassword(), user.getFlag(), user.getAddress(), user.getPhone(), user.getSex(), user.getBirth(), user.getId());
    }

    /**
     * 删除用户
     * @author : oath
     * @create time: 2024/5/14 22:43
     * @param integer id
     * @return int
     */
    public int delete(Integer integer) {

        return BaseDao.executeUpdate("delete from user where id = ?", integer);
    }

    /**
     * 根据id修改用户密码
     * @author : oath
     * @create time: 2024/5/28 22:09
     * @param user 用户实体类
     * @return  int
     */
    public int updatePasswordById(User user) {
        log.info("修改密码：{}", user);
        return BaseDao.executeUpdate("update user set password = ? where id = ?", user.getPassword(), user.getId());
    }
}